﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class uicontrol_SP_BuocGiaCong : System.Web.UI.UserControl
{
    #region Page_Load()
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            LoadSP();
        }
        catch (Exception ex)
        {
            Response.Redirect("adminRender.aspx?mod=dnTaoMoiSP");
        }
    } 
    #endregion

    #region LoadSP()
    public void LoadSP()
    {
        DataTable dt = ((DataView) dsSP.Select(DataSourceSelectArguments.Empty)).Table;
        lblHD.Text = dt.Rows[0]["TenHoiDong"].ToString();
        lblTenSP.Text = dt.Rows[0]["TenSanPham"].ToString();
        Session["MaHoiDong"] = dt.Rows[0]["MaHoiDong"].ToString();
        dt.Dispose();
    }
    #endregion

    #region grvBGC_RowDataBound()
    protected void grvBGC_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) && (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header))
        {
            //Giấu đi cột Mã Bước gia công
            e.Row.Cells[0].Visible = false;         
        }
        if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) && (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header))
        {
            //Giấu đi cột Mã Sản phẩm
            e.Row.Cells[2].Visible = false;
        }
        if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) && (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header))
        {
            //Giấu đi cột Mã Hội đồng
            e.Row.Cells[3].Visible = false;
        }
        if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) && (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header))
        {
            //Giấu đi cột Mã Thiết bị
            e.Row.Cells[4].Visible = false;
        }
        //Thêm Comfirm Message Box vào cho nút xóa
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ImageButton ibDel = (ImageButton) e.Row.FindControl("xoa");
            ibDel.Attributes.Add("onclick","javascript:return confirm('Bạn muốn xóa Bước gia công " + DataBinder.Eval(e.Row.DataItem,"TenBuocGiaCong") + "')");
        }
    } 
    #endregion
    
    #region btnNew_Click()
    protected void btnNew_Click(object sender, EventArgs e)
    {
        dsBGC.Insert();
        dsBGC.DataBind();
        grvBGC.DataBind();

        btnNew.Enabled = false;
    } 
    #endregion

    #region grvBGC_SelectedIndexChanging()
    protected void grvBGC_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        grvBGC.SelectedIndex = e.NewSelectedIndex;
        Bind_grvBGC();
    } 
    #endregion

    #region Reset_Data()
    public void Reset_Data()
    {
        txtTenBGC.Text = "";
        txtDescr.Text = "";
        ddlTB.SelectedIndex = 0;

        btnNew.Enabled = true;
        btnNew.Visible = true;
        btnUpdate.Visible = false;

        lblResult.Text = "";

        Bind_grvBGC();
    }    
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Reset_Data();
    }
    #endregion    
    
    #region grvBGC_RowCommand()
    protected void grvBGC_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Xoa")
        {            
            dsBGC.UpdateParameters.Add("MaBuocGiaCong", TypeCode.Int32,e.CommandArgument.ToString());
            dsBGC.Update();

            Bind_grvBGC();
        }
        if (e.CommandName == "Sua")
        {
            btnNew.Visible = false;
            btnUpdate.Visible = true;
            Session["MaBuocGiaCong"] = e.CommandArgument;
            //dsBGCEdit.SelectParameters.Add("MaBuocGiaCong", TypeCode.Int32, e.CommandArgument.ToString());
            DataTable dt = ((DataView)dsBGCEdit.Select(DataSourceSelectArguments.Empty)).Table;
            txtTenBGC.Text = dt.Rows[0]["TenBuocGiaCong"].ToString();
            txtDescr.Text = dt.Rows[0]["GhiChu"].ToString();
            ddlTB.SelectedValue = dt.Rows[0]["MaThietBi"].ToString();
            lblHD.Text = dt.Rows[0]["TenHoiDong"].ToString();
            lblTenSP.Text = dt.Rows[0]["TenSanPham"].ToString();

            dt.Dispose();
        }
    } 
    #endregion

    #region Bind_dsBGC()
    public void Bind_grvBGC()
    {
        dsBGC.DataBind();
        grvBGC.DataBind();
    }
    #endregion

    #region btnUpdate_Click()
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        dsBGCEdit.Update();        
        Bind_grvBGC();
        lblResult.Text = "Cập nhật thành công.";
    } 
    #endregion
}
